package com.bjh.serialization;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableSerializer {

    public static Object deserialize(byte[] bytes) {
        try{
            ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Object object = inputStream.readObject();
            return  object;
        }catch (Exception e) {
            return null;
        }
    }

    public static byte[] serialize(Object serializable) {
        try{
            ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
            ObjectOutputStream stream = new ObjectOutputStream(byteArrayOS);
            stream.writeObject(serializable);
            stream.close();
            return byteArrayOS.toByteArray();
        }catch (Exception e) {
            return null;
        }
    }
}
